package com.cabrida.livestar.mainstar;

import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.cabrida.livestar.R;
import com.cabrida.livestar.util.Bimp;
import com.cabrida.livestar.util.FileBitmapUtils;

/**
 * 发动态图片adapter
 * 
 * @author why
 * 
 */
public class StarSendTrendGvAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater inflater; // 视图容器
	private int selectedPosition = -1;// 选中的位置
	private boolean shape;
	public static final int tatalImageNum = 9;

	public StarSendTrendGvAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
		this.mContext = context;
	}

	public boolean isShape() {
		return shape;
	}

	public void setShape(boolean shape) {
		this.shape = shape;
	}

	public void update() {
		loading();
	}

	public int getCount() {
		return (Bimp.bmp.size() + 1);
	}

	public Object getItem(int arg0) {

		return null;
	}

	public long getItemId(int arg0) {

		return 0;
	}

	public void setSelectedPosition(int position) {
		selectedPosition = position;
	}

	public int getSelectedPosition() {
		return selectedPosition;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_sendtrend_grida,
					parent, false);
			holder = new ViewHolder();
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_grida_image);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position == Bimp.bmp.size()) {
			holder.image.setImageBitmap(BitmapFactory.decodeResource(
					mContext.getResources(), R.drawable.icon_addpic_unfocused));
			if (position == tatalImageNum) {
				holder.image.setVisibility(View.GONE);
			} else {
				holder.image.setVisibility(View.VISIBLE);
			}
		} else {
			holder.image.setVisibility(View.VISIBLE);
			holder.image.setImageBitmap(Bimp.bmp.get(position));
		}
		return convertView;
	}

	class ViewHolder {
		ImageView image;
	}
	
	Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };
	
	 public void loading() {
         new Thread(new Runnable() {
             public void run() {
                 while (true) {
                     if (Bimp.max == Bimp.drr.size()) {
                         Message message = new Message();
                         message.what = 1;
                         handler.sendMessage(message);
                         break;
                     } else {
                         try {
                             String path = Bimp.drr.get(Bimp.max);
                             System.out.println(path);
                             Bitmap bm = Bimp.revitionImageSize(path);
                             Bimp.bmp.add(bm);
                             String newStr = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
                             FileBitmapUtils.saveBitmap(bm, "" + newStr);
                             Bimp.max += 1;
                             Message message = new Message();
                             message.what = 1;
                             handler.sendMessage(message);
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         }).start();
     }

}
